#include <bits/stdc++.h>
// 2025/01/23
// tag: 
// Author: Zhang Muen
using namespace std;

int n, nums[32768];

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> nums[i];
    int64_t ans = nums[1];
    for (int i = 2; i <= n; i++){
        int m = INT_MAX;
        for (int j = 1; j < i; j++)
            m = min(m, abs(nums[i] - nums[j]));
        ans += m;
    }
    cout << ans;
    return 0;
}